package controller

import (
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/gorm"
	"log"
	"net/http"
	"yangbo/common"
	"yangbo/common/dto"
	"yangbo/model"
	"yangbo/response"
	"yangbo/util"
)

func Register(ctx *gin.Context) {
	DB := common.GetDB()
	//获取参数
	collage := ctx.PostForm("Collage")
	major := ctx.PostForm("Major")
	myclass := ctx.PostForm("Myclass")
	name := ctx.PostForm("Name")
	studentnu := ctx.PostForm("StudentNu")
	password := ctx.PostForm("password")
	character := ctx.PostForm("Character")
	//数据验证
	if len(studentnu) != 10 {
		response.Respomse(ctx, http.StatusUnprocessableEntity, 442, nil, "学号必须是10位")
		return
	}
	if len(password) < 6 {
		response.Respomse(ctx, http.StatusUnprocessableEntity, 442, nil, "密码不能少于6位")
		return
	}
	//如果无名字，直接传一个
	if len(name) == 0 {
		name = util.RandomString(10)
	}

	log.Println(name, studentnu, password)
	//判断学号是否存在
	if isStudentNuExist(DB, studentnu) {
		response.Respomse(ctx, http.StatusUnprocessableEntity, 442, nil, "用户已经存在")
		return
	}
	//创建用户
	//hasedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
	//if err != nil {
	//	response.Respomse(ctx, http.StatusUnprocessableEntity, 442, nil, "加密错误") //系统错误
	//	return
	//}
	newUser := model.User{
		Collage:   collage,
		Major:     major,
		Myclass:   myclass,
		Name:      name,
		StudentNu: studentnu,
		Password:  password,
		Character: character,
	}
	DB.Create(&newUser)
	//返回结果
	response.Success(ctx, nil, "注册成功")
}

func Login(ctx *gin.Context) {
	DB := common.GetDB()
	//获取参数
	//collage := ctx.PostForm("Collage")
	//major 	:= ctx.PostForm("Major")
	//myclass := ctx.PostForm("Myclass")
	//name := ctx.PostForm("Name")
	studentnu := ctx.PostForm("StudentNu")
	password := ctx.PostForm("password")

	//fmt.Println(len(collage))

	//数据验证
	//if len(collage) == 0{
	//	response.Respomse(ctx, http.StatusUnprocessableEntity, 442, nil, "学院必须存在")
	//	return
	//
	//}
	//if len(major)  ==0{
	//	response.Respomse(ctx, http.StatusUnprocessableEntity, 442, nil, "专业必须存在")
	//	return
	//}
	//if len(myclass) ==0{
	//	response.Respomse(ctx, http.StatusUnprocessableEntity, 442, nil, "学号必须是10位")
	//	return
	//}
	//if len(name) == 0{
	//	response.Respomse(ctx, http.StatusUnprocessableEntity, 442, nil, "学号必须是10位")
	//	return
	//}
	if len(studentnu) != 10 {
		response.Respomse(ctx, http.StatusUnprocessableEntity, 442, nil, "学号必须是10位")
		return
	}
	if len(password) < 6 {
		response.Respomse(ctx, http.StatusUnprocessableEntity, 442, nil, "密码不能少于6位")
		//ctx.JSON(http.StatusUnprocessableEntity, gin.H{"code": 442, "msg": "密码不能少于6位"})
		return
	}
	//判断学号是否存在
	var user model.User
	DB.Where("student_nu=?", studentnu).First(&user)
	if user.ID == 0 {
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{"code": 422, "msg": "用户不存在"})
		return
	}
	//判断密码是否正确
	//if err := []byte(password); err != nil{
	//	ctx.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "密码错误"})
	//	return
	//}
	if password != user.Password {
		ctx.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "密码错误"})
		return
	}

	//发放token
	token, err := common.ReleaseToken(user)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"code": 500, "msg": "系统异常"})
		log.Printf("token generate error : %v", err)
		return
	}

	//返回结果
	response.Success(ctx, gin.H{"token": token}, "登录成功")
}

func Info(ctx *gin.Context) {
	user, _ := ctx.Get("user")

	ctx.JSON(http.StatusOK, gin.H{"code": 200, "data": gin.H{"user": dto.TouserDto(user.(model.User))}})
}

//查询学号是否存在
func isStudentNuExist(db *gorm.DB, studentnu string) bool {
	var user model.User
	db.Where("student_nu=?", studentnu).First(&user)
	if user.ID != 0 {
		return true
	}
	return false
}
